package com.erps.CommonUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erps.db.DBManager;

public class PageBean {
	private static final Logger log = LoggerFactory.getLogger(PageBean.class);

	private PageBean() {

	}

	public static int getDisplayMode(String cOrgID) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select keyvalue ");
		sql.append(" from s_orginf_config ");
		sql.append(" where orgid='" + cOrgID + "'");
		sql.append(" and KEYNAME='DISPLAY_SUMMARY' ");
		String cMode = DBManager.GetStrValue(sql.toString());
		return StringEx.getInt(cMode);
	}

	public static String getFacade(String cOrgID, String cPageID) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select facade ");
		sql.append(" from t_page_" + cOrgID);
		sql.append(" where pageid='" + cPageID + "'");
		return DBManager.GetStrValue(sql.toString());
	}

	public static List<Map<String, String>> getAlarmWords(String cOrgID) {
		StringBuffer sql = new StringBuffer();
		sql.append("select clsid,clsname ");
		sql.append("from usercls_info ");
		sql.append(" where orgid='" + cOrgID + "'");
		sql.append(" and actionid=1002 ");
		return DBManager.QueryData(sql.toString());
	}

	public static String getAlarmClsType(String cOrgID, String vClsID) {
		StringBuffer sql = new StringBuffer();
		sql.append("select clsname ");
		sql.append("from usercls_info ");
		sql.append(" where orgid='" + cOrgID + "'");
		sql.append(" and clsid='" + vClsID + "'");
		return DBManager.GetStrValue(sql.toString());
	}

	public static List<G_KeyValue> getFacadeList() {
		String sql = AppManager.getDictionarySQLExt("135");
		return DBManager.QueryList(sql);
	}

	public static int getUrlTypeID(String cPageID) {
		if (cPageID != null)
			return StringEx.getInt(cPageID.substring(22, 24));
		else
			return 11;
	}

	public static String getFacadeSelect(String cFacadeID) {
		StringBuffer sb = new StringBuffer();
		List<G_KeyValue> dtRows = getFacadeList();
		int iLength = (dtRows == null) ? 0 : dtRows.size();
		String cv = "";
		for (int i = 0; i < iLength; i++) {
			G_KeyValue vo = dtRows.get(i);
			String cKeyID = vo.getKeyValue();
			if (cFacadeID.equals(cKeyID)) {
				cv = "<option value='" + cKeyID + "' selected>" + vo.getKeyName() + "</option>";
			} else {
				cv = "<option value='" + cKeyID + "'>" + vo.getKeyName() + "</option>";
			}
			sb.append(cv);
		}
		return sb.toString();
	}

	public static List<String> getAlarmText(String cOrgID) {
		List<Map<String, String>> arKeys = getAlarmWords(cOrgID);
		int iLength = (arKeys == null) ? 0 : arKeys.size();
		ArrayList<String> aKeys = new ArrayList<String>(iLength);
		if (iLength > 0) {
			for (Map<String, String> x : arKeys) {
				String cAlarmText = x.get("clsname");
				aKeys.add(cAlarmText);
			}
		}
		return aKeys;
	}

	public static Boolean getISAlarm(String cOrgID, String cBody) {
		List<Map<String, String>> arKeys = getAlarmWords(cOrgID);
		int iLength = (arKeys == null) ? 0 : arKeys.size();
		Boolean isAllowAlarm = false;
		if (iLength > 0) {
			for (Map<String, String> x : arKeys) {
				String cAlarmText = x.get("clsname");
				if (StringUtils.indexOfIgnoreCase(cBody, cAlarmText) != -1) {
					isAllowAlarm = true;
					break;
				}
			}
		}
		return isAllowAlarm;
	}

	public static String getURLDecoder(String cStr) {
		try {
			return URLDecoder.decode(cStr, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return cStr;
		}
	}

	public String getURLEncoder(String cStr) {
		try {
			return URLEncoder.encode(cStr, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return cStr;
		}
	}

}
